package cloudoer.blog.service.impl;

import cloudoer.blog.exception.ServiceException;
import cloudoer.blog.service.EmailService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.sql.rowset.serial.SerialException;

@Service("emailService")
public class EmailServiceImpl implements EmailService {

    private final String MY_EMAIL = "cloudoer@foxmail.com";

    @Resource(name = "javaMail")
    private JavaMailSender javaMailSender;


    public void sendMail(String email,String id) throws ServiceException{
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message,true,"utf-8");
            helper.setFrom(MY_EMAIL);
            helper.setTo(email);
            helper.setSubject("cloudoer");
            String text = "你即将成为本站最尊贵的VIP会员啦，你的到来让本站蓬荜生辉，快点击下面链接进入新世界吧！" +
                    "<br><a href='http://baidu.com?id="+id+"'>http://baidu.com?id="+id+"</a>" +
                    "<br>如果你点不进去的话就只好委屈你亲自动手把链接复杂到浏览器里面打开喽";
            helper.setText(text,true);
            javaMailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new ServiceException("我好像找不到你给我的邮箱地址");
        }
    }


}
